home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / swtools / generic / filestuff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  9.0 KB  |  378 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <strings.h>
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24. #include <pwd.h>
  25. #include <fcntl.h>
  26. #include "generic.h"
  27. #include <gl.h>
  28.  
  29. static char newfilename[300];
  30. static char expandedname[300];
  31. char currentpath[300];
  32.  
  33. char currentfilename[300];
  34.  
  35. /* flags: */
  36.  
  37. long    VIEW_ONLY = 0, view_only = 0;
  38. long    MAKE_BACKUPS = 0;
  39. long    foreground_flag = 0, dirtyfile_flag = 0;
  40.  
  41. long    xorg, yorg, xsize, ysize;
  42.  
  43. /*
  44.  * Expand ~ to home dorectory
  45.  */
  46.  
  47. static char *expandtilde(char *exname, char *file)
  48. {
  49.     char *c, *p;
  50.     struct passwd *pw;
  51.     char login[40];
  52.  
  53.     if (file[0] != '~')
  54.         return (strcpy(exname, file));
  55.  
  56.     for (p = login, c = &file[1]; *c && *c != '/'; *p++ = *c++)
  57.         ;
  58.     *p = '\0';
  59.     if (login[0] == '\0')
  60.         (void) strcpy(exname, getenv("HOME"));
  61.     else {
  62.         pw = getpwnam(login);
  63.         if (pw == 0)
  64.             return (0);
  65.         (void) strcpy(exname, pw->pw_dir);
  66.     }
  67.     (void) strcat(exname, c);
  68.     return exname;
  69. }
  70.  
  71. long openstatus(char *filename)
  72. {
  73.     struct stat buf;
  74.     long    uid, gid;
  75.     int            fi;
  76.  
  77.     if (expandtilde(expandedname, filename) == 0) return 0;
  78.     
  79.     uid = geteuid();
  80.     gid = getegid();
  81.     if (-1 == stat(expandedname, &buf)) {
  82.     if (-1 == (fi = creat(expandedname, (mode_t)0664)))
  83.         return 0;
  84.     (void)close(fi);
  85.     (void)unlink(expandedname);
  86.     return CAN_WRITE_FILE;
  87.     }
  88.     if ((buf.st_mode & S_IFREG) == 0)
  89.     return FILE_EXISTS;
  90.     if ((buf.st_uid == uid && (buf.st_mode & S_IWRITE)) ||
  91.     (buf.st_gid == gid && (buf.st_mode & S_IWGRP)) ||
  92.     (buf.st_mode & S_IWOTH))
  93.         return FILE_EXISTS | CAN_WRITE_FILE;
  94.     else
  95.     return FILE_EXISTS;
  96. }
  97.  
  98. void makewintitle()
  99. {
  100.     char title[220], *tail;
  101.  
  102.     if (*currentfilename)
  103.     (void)sprintf(title, "%s (ver %s): %s", PROG_NAME, PROG_VERSION, currentfilename);
  104.     else
  105.     (void)sprintf(title, "%s (ver %s): (no file name)", PROG_NAME, PROG_VERSION);
  106.     if (dirtyfile_flag) (void)strcat(title, "*");
  107.     if (view_only)
  108.     (void)strcat(title, "[View Only]");
  109.     wintitle(title);
  110.     tail = strrchr(currentfilename, '/');
  111.     tail = (tail == NULL) ? currentfilename: tail+1;
  112.     icontitle(tail);
  113. }
  114.  
  115. /* setdirtyfile(), cleardirtyfile() mark the file dirty and clean
  116.  *  and resets the window title, if necessary.
  117.  */
  118.  
  119. void setdirtyfile()
  120. {
  121.     if (dirtyfile_flag == 0) {
  122.     dirtyfile_flag = 1;
  123.     makewintitle();
  124.     }
  125. }
  126.  
  127. void cleardirtyfile()
  128. {
  129.     if (dirtyfile_flag == 1) {
  130.     dirtyfile_flag = 0;
  131.     makewintitle();
  132.     }
  133. }
  134.  
  135. /* checkfile() returns 0 if the filename is not to be put in the
  136.  * browsegizmo.  This default version shows all file names.
  137.  */
  138.  
  139. long checkfile(char *filename)
  140. {
  141.     return (*filename != 0);
  142. }
  143.  
  144. long collectfilename(char *label, char *newname)
  145. {
  146.     if (1 == getfilename(label, newname, 0, currentpath, "Open", checkfile))
  147.         return 1;
  148.     return 0;
  149. }
  150.  
  151. long dirtyfilecheck()
  152. {
  153.     if (dirtyfile_flag) {
  154.     switch(message("File modified; save the changes?",
  155.                                     "Yes", "No", "Cancel")) {
  156.         case 1:
  157.             handlesavecmd();
  158.         if (dirtyfile_flag) return 0;
  159.         return 1;
  160.         case 2:
  161.         return 1;
  162.         case 3:
  163.             return 0;
  164.     }
  165.     }
  166.     return 1;
  167. }
  168.  
  169. void handlenewcmd()
  170. {
  171.     if (VIEW_ONLY) {
  172.     (void)message("New not allowed in view only mode", 0, 0, 0);
  173.     return;
  174.     }
  175.     if (dirtyfilecheck() == 0) return;
  176.     if (entries.clearbuffer) entries.clearbuffer();
  177.     dirtyfile_flag = 0;
  178.     view_only = 0;
  179.     currentfilename[0] = 0;
  180.     makewintitle();
  181.     return;
  182. }
  183.  
  184. void doopen(char *filename)
  185. {
  186.     switch (openstatus(filename)) {
  187.     case 0:
  188.         if (VIEW_ONLY) {
  189.             (void)message("No such file", 0, 0, 0);
  190.             return;
  191.         }
  192.         (void)message("Non-existant, unwriteable file\n", 0, 0, 0);
  193.         return;
  194.     case FILE_EXISTS:
  195.         if (entries.clearbuffer) entries.clearbuffer();
  196.         view_only = 1;
  197.         (void)strcpy(currentfilename, expandedname);
  198.         if (entries.readfile) entries.readfile(currentfilename);
  199.         dirtyfile_flag = 0;
  200.         break;
  201.     case CAN_WRITE_FILE:
  202.         if (VIEW_ONLY) {
  203.             (void)message("No such file", 0, 0, 0);
  204.             return;
  205.         }
  206.         (void)message("New file", 0, 0, 0);
  207.         if (VIEW_ONLY == 0) view_only = 0;
  208.         if (entries.clearbuffer) entries.clearbuffer();
  209.         (void)strcpy(currentfilename, expandedname);
  210.         dirtyfile_flag = 0;
  211.         break;
  212.     case FILE_EXISTS | CAN_WRITE_FILE:
  213.         if (VIEW_ONLY == 0) view_only = 0;
  214.         if (entries.clearbuffer) entries.clearbuffer();
  215.         (void)strcpy(currentfilename, expandedname);
  216.         if (entries.readfile) entries.readfile(currentfilename);
  217.         writebackupfile();
  218.         dirtyfile_flag = 0;
  219.         break;
  220.     }
  221. }
  222.  
  223. void handleopencmd()
  224. {
  225.     if (dirtyfilecheck() == 0) return;
  226.     if (collectfilename("Open File:", newfilename) == 0) return;
  227.     doopen(newfilename);
  228.     makewintitle();
  229. }
  230.  
  231. void handlesavecmd()
  232. {
  233.     if (view_only) {
  234.         (void)message("View only mode", 0, 0, 0);
  235.     return;
  236.     }
  237.     if (currentfilename[0] == 0) {
  238.     if (collectfilename("Save As:", newfilename) == 0) return;
  239.     if (openstatus(newfilename) & CAN_WRITE_FILE) {
  240.         (void)strcpy(currentfilename, expandedname);
  241.         } else {
  242.         (void)message("Can't write file", 0, 0, 0);
  243.         return;
  244.     }
  245.     } else if ((openstatus(currentfilename) & CAN_WRITE_FILE) == 0) {
  246.     view_only = 1;
  247.     (void)message("Can't write file", 0, 0, 0);
  248.     return;
  249.     }
  250.     if (entries.writefile)
  251.         if (entries.writefile(currentfilename) == 0) return;
  252.     dirtyfile_flag = 0;
  253.     makewintitle();
  254. }
  255.  
  256. void handlesaveascmd()
  257. {
  258.     if (collectfilename("Save As:", newfilename) == 0) return;
  259.     if (openstatus(newfilename) & CAN_WRITE_FILE) {
  260.     (void)strcpy(currentfilename, expandedname);
  261.     } else {
  262.     (void)message("Can't write file", 0, 0, 0);
  263.     return;
  264.     }
  265.     if (entries.writefile)
  266.         if (entries.writefile(currentfilename) == 0) return;
  267.     dirtyfile_flag = 0;
  268.     if (VIEW_ONLY == 0) view_only = 0;
  269.     makewintitle();
  270. }
  271.  
  272. void handleinsertcmd()
  273. {
  274.     if (view_only) {
  275.         (void)message("View only mode", 0, 0, 0);
  276.     return;
  277.     }
  278.     if (collectfilename("Insert File:", newfilename) == 0) return;
  279.     if (openstatus(newfilename) & FILE_EXISTS) {
  280.     if (entries.insertfile) entries.insertfile(newfilename);
  281.     } else {
  282.     (void)message("Can't read file", 0, 0, 0);
  283.     }
  284.     makewintitle();
  285. }
  286.  
  287. void handlequitcmd()
  288. {
  289.     if (dirtyfilecheck() == 0) return;
  290.     if (entries.quit) entries.quit();
  291.     exit(0);
  292. }
  293.  
  294. void handlehelpcmd()
  295. {
  296.     if (entries.help)
  297.         entries.help();
  298.     else
  299.         (void)message("No help available", 0, 0, 0);
  300. }
  301.  
  302. static inerror = 0;
  303.  
  304. void handleerrorwrite(long sigtype)
  305. {
  306.     char filename[100], msg[200];
  307.     char *tmpdir;
  308.  
  309.     if (inerror) return;    /* Hopeless -- error occurred during write */
  310.     inerror = 1;
  311.     tmpdir = (char *)getenv("TMPDIR");
  312.     if (tmpdir == 0) tmpdir = "/tmp";
  313.     (void)sprintf(filename, "%s/%s.crash", tmpdir, PROG_NAME);
  314.     (void)sprintf(msg, "Yer gonna crash (%d): Try to save as %s?",
  315.                                                     (int)sigtype, filename);
  316.     if (message(msg, "Yes", "No", 0) == 1)
  317.         if (entries.errorwritefile) entries.errorwritefile(filename);
  318. }
  319.  
  320. /* print stuff: generate a print name from the pid and
  321.  * an increasing number.  Put the result in $TMPDIR.
  322.  */
  323.  
  324. static long printindex = 1;
  325.  
  326. void handleprintcmd()
  327. {
  328.     char printfilename[100];
  329.     char *tmpdir;
  330.  
  331.     long pid = (long)getpid();
  332.     tmpdir = (char *)getenv("TMPDIR");
  333.     if (tmpdir == 0) tmpdir = "/tmp";
  334.     (void)sprintf(printfilename, "%s/%s%4.4x%4.4x", tmpdir, PROG_NAME, (int)pid, (int)printindex++);
  335.     if (entries.printfile)
  336.         if (0 == entries.printfile(printfilename)) {
  337.         (void)message("Print failed", "Confirm", 0, 0);
  338.     }
  339. }
  340.  
  341. static void defaultmakecheckpointname(char *s)
  342. {
  343.     (void)sprintf(s, "%s.ckp", currentfilename);
  344. }
  345.  
  346. static void defaultmakebackupname(char *s)
  347. {
  348.     (void)sprintf(s, "%s.bak", currentfilename);
  349. }
  350.  
  351. void writecheckpointfile()
  352. {
  353.     char filename[300];
  354.  
  355.     if (currentfilename[0] == 0) return;
  356.     if (entries.makecheckpointname)
  357.     entries.makecheckpointname(filename);
  358.     else
  359.     defaultmakecheckpointname(filename);
  360.     if ((openstatus(filename) & CAN_WRITE_FILE) && entries.writefile)
  361.     entries.writefile(filename);
  362. }
  363.  
  364. void writebackupfile()
  365. {
  366.     char filename[300];
  367.  
  368.     if (MAKE_BACKUPS) {
  369.         if (currentfilename[0] == 0) return;
  370.     if (entries.makebackupname)
  371.         entries.makebackupname(filename);
  372.     else
  373.         defaultmakebackupname(filename);
  374.     if ((openstatus(filename) & CAN_WRITE_FILE) && entries.writefile)
  375.         entries.writefile(filename);
  376.     }
  377. }
  378.